package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yidiandao.business.R;
import com.yidiandao.business.data.recyclertype.AccurateContent;
import com.yidiandao.business.data.recyclertype.SearchAccurateContent;
import com.yidiandao.business.ui.main.discovery.searchaccurate.DiscoverySearchAccurateActivity;
import com.yidiandao.business.ui.widget.view.SearchFlowLayout;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 精确搜索指定内容-Provider
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/4/12 0012 18:54
 */
public class DiscoverySearchAccurateProvider extends ItemViewProvider<SearchAccurateContent, DiscoverySearchAccurateProvider.ViewHolder> {

    private Activity mActivity;

    public DiscoverySearchAccurateProvider(Activity activity) {
        mActivity = activity;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_discovery_search_assign, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final SearchAccurateContent data) {
        final List<AccurateContent> contents = data.getContent();
        List<String> accurateContents = new ArrayList<>();
        for (AccurateContent content : contents) {
            accurateContents.add(content.getName());
        }
        holder.searchFlowLayout.setData(accurateContents);
        holder.searchFlowLayout.setOnSearchTagClickListener(new SearchFlowLayout.OnSearchTagClickListener() {
            @Override
            public void onSearchTagClick(String text, int position) {
                toSearchAccurateActivity(contents.get(position));
            }
        });
    }

    private void toSearchAccurateActivity(AccurateContent accurateContent) {
        DiscoverySearchAccurateActivity.actionStart(mActivity, accurateContent);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private SearchFlowLayout searchFlowLayout;

        public ViewHolder(View itemView) {
            super(itemView);

            searchFlowLayout = (SearchFlowLayout) itemView.findViewById(R.id.search_flow_layout);
        }
    }
}
